package com.bywyu.turnpage.view;

import android.content.Context;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SufaceCubeView extends SurfaceView implements SurfaceHolder.Callback {
	
	private DrawThread dt;
	
	public SufaceCubeView(Context context) {
		super(context);
		
	}
	
	

	public void destory(){
		
	}



	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		
	}
	
	public void startAnimation() {
		if (dt == null) {
			dt = new DrawThread(getHolder());
			dt.start();
		}
	}

	public void stopAnimation() {
		if (dt != null) {
			dt.flag = false;
			Thread t = dt;
			dt = null;
			t.interrupt();
		}
	}
	
	public class DrawThread extends Thread {
		SurfaceHolder surfaceHolder;
		boolean flag = false; 
		int sleepSpan = 30; 

		public DrawThread(SurfaceHolder surfaceHolder) {
			this.surfaceHolder = surfaceHolder; 
			this.flag = true; 
		}

		public void run() {
			Canvas canvas = null;
			while (flag) {
				try {
					canvas = surfaceHolder.lockCanvas(null);
					if (canvas == null)
						continue;
						synchronized (surfaceHolder) {
							//操作
					}
				} catch (Exception e) {
					e.printStackTrace(); 
				} finally {
					if (canvas != null) {
						surfaceHolder.unlockCanvasAndPost(canvas);
					}
				}
				try {
					Thread.sleep(sleepSpan); 
				} catch (Exception e) {
					e.printStackTrace(); 
				}
			}
		}
	}
}
